<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('after_sales_logistics', function (Blueprint $table) {
            $table->id();
            $table->foreignId('request_id')->constrained('after_sales_requests')->onDelete('cascade')->comment('售后申请ID');
            
            // 物流类型
            $table->enum('type', ['return', 'exchange'])->comment('物流类型：return-退货物流，exchange-换货物流');
            $table->string('submitted_by', 20)->default('user')->comment('提交者：user-用户，admin-管理员');
            
            // 物流信息
            $table->string('tracking_number')->comment('物流单号');
            $table->string('company')->comment('物流公司');
            $table->string('status')->default('pending')->comment('物流状态');
            
            // 时间记录
            $table->timestamp('sent_at')->nullable()->comment('发货时间');
            $table->timestamp('received_at')->nullable()->comment('收货时间');
            
            // 地址信息
            $table->string('sender_name')->nullable()->comment('寄件人姓名');
            $table->string('sender_phone')->nullable()->comment('寄件人电话');
            $table->text('sender_address')->nullable()->comment('寄件人地址');
            $table->string('receiver_name')->nullable()->comment('收件人姓名');
            $table->string('receiver_phone')->nullable()->comment('收件人电话');
            $table->text('receiver_address')->nullable()->comment('收件人地址');
            
            // 备注
            $table->text('note')->nullable()->comment('备注');
            
            $table->timestamps();
            
            // 索引
            $table->index(['request_id', 'type']);
            $table->index('tracking_number');
            $table->unique(['request_id', 'type', 'submitted_by'], 'after_sales_logistics_unique_submission');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('after_sales_logistics');
    }
};
